//
//  YWNoQrcodeLedgerEquipmentTableViewCell.m
//  maintain
//
//  Created by 烟雨云渊 on 16/12/5.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWNoQrcodeLedgerEquipmentTableViewCell.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
//#import "UILabel+AttributedText.h"

#import "YWEquipment.h"
#import "YWDBTool.h"
#import "YWCommonTool.h"
#import "UIImageView+WebCache.h"
#import "YWLastMaintenanceTime.h"
#import "YWEquipmentManager.h"
#import "YWMeterReadingManager.h"
#import "YWPicture.h"
#import "YWMissionManager.h"
#import "YYLabel.h"

@interface YWNoQrcodeLedgerEquipmentTableViewCell()
@property (nonatomic, strong) UIImageView* equipmentImageView;
//设备名称
@property (nonatomic, strong)YYLabel *equipmentLabel;
//自编码
@property (nonatomic, strong)YYLabel *ownCodeLabel;
//型号
@property (nonatomic, strong)YYLabel *modelLabel;
//厂商
@property (nonatomic, strong)YYLabel *manufacturersLabel;
/**生产日期*/
@property (nonatomic, strong)YYLabel *productDateLabel;
//安装日期
@property (nonatomic, strong)YYLabel *installDateLabel;
//上次检测时间
//@property (nonatomic, weak)YYLabel *lastMaintenceTimeLabel;
//使用年限
@property (nonatomic, weak)YYLabel *userYearLabel;
@end

@implementation YWNoQrcodeLedgerEquipmentTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setEquipment:(YWEquipment *)equipment{
    _equipment = equipment;
    NSArray* pictures = [[YWMissionManager shareManager] picturesWithEquipmentId:equipment.equipmentId pictureTroubleType:YWPictureTroubleTypeEquipment];
    equipment.pictures = pictures;
    YWPicture *picture = equipment.pictures.count>0?equipment.pictures[0]:nil;
    NSString* downloadUrl = [YWCommonTool pictureDownloadUrl:picture.smallPictureUrl];
    [self.equipmentImageView sd_setImageWithURL:[NSURL URLWithString:downloadUrl] placeholderImage:[UIImage imageNamed:@"image_placeholder"]];
    
    self.equipmentLabel.text = equipment.equipmentName.length?equipment.equipmentName:@"未填写";
    
    self.ownCodeLabel.text = [NSString stringWithFormat:@"%@",equipment.ownCode.length?equipment.ownCode:@"未填写"];
    self.modelLabel.text = [NSString stringWithFormat:@"型号: %@",equipment.model.length?equipment.model:@"未填写"];
    self.manufacturersLabel.text = [NSString stringWithFormat:@"厂商: %@",equipment.factoryName.length?equipment.factoryName:@"未填写"];
    
    NSInteger eEquipmentTypeId = [[YWMeterReadingManager shareManager] electricMeterEquipmentTypeId];
    NSInteger wEquipmentTypeId = [[YWMeterReadingManager shareManager] waterMeterEquipmentTypeId];
    NSInteger gEquipmentTypeId = [[YWMeterReadingManager shareManager] gasMeterEquipmentTypeId];
    if (equipment.equipmentTypeId == eEquipmentTypeId || equipment.equipmentTypeId == wEquipmentTypeId || equipment.equipmentTypeId == gEquipmentTypeId) {
        self.productDateLabel.text = [self companyName];
        self.installDateLabel.text = [self place];
        self.userYearLabel.text = [self rate];
        self.productDateLabel.hidden = NO;
        self.userYearLabel.hidden = NO;
    }else{
//        NSString * productDateStr = equipment.produceDate>0?[YWCommonTool showDate:equipment.produceDate/1000]:@"未填写";
//        self.productDateLabel.text = [NSString stringWithFormat:@"生产日期: %@",productDateStr];
        NSString * installDateStr = equipment.installDate>0?[YWCommonTool showDate:equipment.installDate/1000]:@"未填写";
        self.installDateLabel.text = [NSString stringWithFormat:@"安装日期: %@",installDateStr];
        self.productDateLabel.hidden = YES;
        self.userYearLabel.hidden = YES;
//        NSString *yearStr = equipment.durableYears>0 ? [NSString stringWithFormat:@"%ld年",(long)equipment.durableYears]:@"未填写";
//        self.userYearLabel.text = [NSString stringWithFormat:@"使用年限: %@",yearStr];
    }
    
//    YWLastMaintenanceTime* lastMaintenanceTime = [[YWEquipmentManager shareManager] lastMaintenanceTimeWithEquipmentId:equipment.equipmentId];
//    NSString* maintenanceTime = lastMaintenanceTime.maintenanceDate>0?[YWCommonTool showChineseFormatterDate:lastMaintenanceTime.maintenanceDate / 1000]:@"无";
    
    //    NSString *lastHandler = lastMaintenanceTime.executorName.length>0?lastMaintenanceTime.executorName:@"无";
    //    self.lastMaintenceManLabel.text = [NSString stringWithFormat:@"上次检测人: %@",lastHandler];
//    self.lastMaintenceTimeLabel.text = [NSString stringWithFormat:@"上次检测时间: %@",maintenanceTime];
}

- (NSString *)rate{
    YWMeterProperty *meterProperty = [[YWEquipmentManager shareManager] meterPropertyWithEquipmentId:self.equipment.equipmentId];
    NSString * rate = [NSString stringWithFormat:@"倍率: %.1f",meterProperty.rate>0?meterProperty.rate:1.0];
    return rate;
}

- (NSString *)companyName{
    YWMeterProperty *meterProperty = [[YWEquipmentManager shareManager] meterPropertyWithEquipmentId:self.equipment.equipmentId];
    NSString * companyName = [NSString stringWithFormat:@"单位名称: %@",meterProperty.companyName.length>0 ? meterProperty.companyName:@"无"];
    return companyName;
}

- (NSString *)place{
    YWMeterProperty *meterProperty = [[YWEquipmentManager shareManager] meterPropertyWithEquipmentId:self.equipment.equipmentId];
    NSString * place = [NSString stringWithFormat:@"房号: %@",meterProperty.place.length>0 ? meterProperty.place:@"无"];
    return place;
}

- (void)setUpSubviews{
    [self.equipmentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(10);
        make.left.mas_equalTo(self.contentView).offset(10);
        make.size.mas_equalTo(CGSizeMake(44, 44));
    }];
    
    [self.equipmentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(10);
        make.left.mas_equalTo(self.equipmentImageView.mas_right).offset(10);
        make.right.mas_equalTo(self.contentView).offset(-10);
    }];
    
    [self.ownCodeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.equipmentLabel.mas_bottom).offset(8);
        make.left.mas_equalTo(self.equipmentImageView.mas_right).offset(10);
        make.right.mas_equalTo(self.contentView).offset(-10);
    }];
    
    [self.manufacturersLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.equipmentImageView.mas_bottom).offset(10);
        make.left.mas_equalTo(self.contentView).offset(10);
        make.right.mas_equalTo(self.contentView).offset(-10);
    }];
    
    [self.modelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.manufacturersLabel.mas_bottom).offset(10);
        make.left.mas_equalTo(self.contentView).offset(10);
    }];
    [self.userYearLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.manufacturersLabel.mas_bottom).offset(10);
        make.left.mas_equalTo(self.modelLabel.mas_right).offset(10);
        make.right.mas_equalTo(self.contentView).offset(-10);
    }];
    
    [self.installDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.modelLabel.mas_bottom).offset(10);
        make.left.mas_equalTo(self.contentView).offset(10);
        make.bottom.mas_equalTo(self.contentView).offset(-10);
    }];
    [self.productDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.userYearLabel.mas_bottom).offset(10);
        make.left.mas_equalTo(self.installDateLabel.mas_right).offset(10);
        make.right.mas_equalTo(self.contentView).offset(-10);
        make.bottom.mas_equalTo(self.contentView).offset(-10);
    }];
    
//    [self.lastMaintenceTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.installDateLabel.mas_bottom).offset(10);
//        make.left.mas_equalTo(self.contentView).offset(10);
//        make.right.mas_equalTo(self.contentView).offset(-10);
//        make.bottom.mas_equalTo(self.contentView).offset(-10);
//    }];
}

- (UIImageView *)equipmentImageView{
    if (!_equipmentImageView) {
        UIImageView* equipmentImageView = [[UIImageView alloc] init];
        _equipmentImageView = equipmentImageView;
        [self.contentView addSubview:equipmentImageView];
    }
    return _equipmentImageView;
}

- (YYLabel *)equipmentLabel{
    if (!_equipmentLabel) {
        YYLabel *equipmentLabel = [[YYLabel alloc]init];
        equipmentLabel.font = KTitleFont;
        //        equipmentLabel.numberOfLines = 0;
        _equipmentLabel = equipmentLabel;
        [self.contentView addSubview:equipmentLabel];
    }
    return _equipmentLabel;
}

-(YYLabel *)ownCodeLabel{
    if (!_ownCodeLabel) {
        YYLabel *ownCodeLabel = [[YYLabel alloc]init];
        ownCodeLabel.font = kContentFont;
        [self.contentView addSubview:ownCodeLabel];
        _ownCodeLabel = ownCodeLabel;
    }
    return _ownCodeLabel;
}

-(YYLabel *)modelLabel{
    if (!_modelLabel) {
        YYLabel *modelLabel = [[YYLabel alloc]init];
        modelLabel.font = kContentFont;
        [self.contentView addSubview:modelLabel];
        _modelLabel = modelLabel;
    }
    return _modelLabel;
}

-(YYLabel *)manufacturersLabel{
    if (!_manufacturersLabel) {
        YYLabel *manufacturersLabel = [[YYLabel alloc]init];
        manufacturersLabel.font = kContentFont;
        [self.contentView addSubview:manufacturersLabel];
        _manufacturersLabel = manufacturersLabel;
    }
    return _manufacturersLabel;
}
-(YYLabel *)productDateLabel{
    if (!_productDateLabel) {
        YYLabel *productDateLabel = [[YYLabel alloc]init];
        productDateLabel.font = kContentFont;
        [self.contentView addSubview:productDateLabel];
        _productDateLabel = productDateLabel;
    }
    return _productDateLabel;
}
-(YYLabel *)installDateLabel{
    if (!_installDateLabel) {
        YYLabel *installDateLabel = [[YYLabel alloc]init];
        installDateLabel.font = kContentFont;
        [self.contentView addSubview:installDateLabel];
        _installDateLabel = installDateLabel;
    }
    return _installDateLabel;
}

//-(YYLabel *)lastMaintenceTimeLabel{
//    if (!_lastMaintenceTimeLabel) {
//        YYLabel *lastMaintenceTimeLabel = [[YYLabel alloc]init];
//        lastMaintenceTimeLabel.font = kContentFont;
//        [self.contentView addSubview:lastMaintenceTimeLabel];
//        _lastMaintenceTimeLabel = lastMaintenceTimeLabel;
//    }
//    return _lastMaintenceTimeLabel;
//}
-(YYLabel *)userYearLabel{
    if (!_userYearLabel) {
        YYLabel *userYearLabel = [[YYLabel alloc]init];
        userYearLabel.font = kContentFont;
        [self.contentView addSubview:userYearLabel];
        _userYearLabel = userYearLabel;
    }
    return _userYearLabel;
}

@end
